package config

import (
	"github.com/BurntSushi/toml"
	"log/slog"
)

type Config struct {
	Application Application `toml:"application"`
}

type Application struct {
	ReadTimeout   int    `toml:"read-timeout"`
	WriterTimeout int    `toml:"writer-timeout"`
	Host          string `toml:"host"`
	Port          int64  `toml:"port"`
	AppName       string `toml:"name"`
	Environment   string `toml:"environment"`
}

var config *Config

// LoadConfig 加载配置文件
func LoadConfig(env Environment) (*Config, error) {
	// 加载默认的配置文件
	if _, err := toml.DecodeFile("./config/settings.toml", &config); err != nil {
		slog.Error("加载配置文件失败, 错误原因：", err)
		return nil, err
	}

	// 加载特定环境的配置文件
	// local-开发机，settings.local.toml
	// dev-开发环境，settings.dev.toml
	// test-集成测试环境，settings.test.toml
	// prod-生产环境，settings.prod.toml
	if len(env) > 0 {
		if _, err := toml.DecodeFile("./config/"+GetEnvConfFilename(env), &config); err != nil {
			slog.Error("加载配置文件失败, 错误原因：", err)
			return nil, err
		}
	}
	return config, nil
}
